﻿package view
{
	import controller.GameController;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	public class Score extends MovieClip
	{
		
		private var gameOverSound:Sound = new Sound;
		private var pointsSound:Sound = new Sound;
		private var medalSound:Sound = new Sound;
		private var finishSound:Sound = new Sound;
		private var gameEndSound:Sound = new Sound;
		private var pictureSound:Sound = new Sound;
		
		private var soundChannel:SoundChannel = new SoundChannel();
		private var cart:Cart;
		private var picture:Boolean = false;
		private var buttons:Array;
		private var gameControl:GameController;
		
		
		private var timer:Timer = new Timer(500, 1);
		
		public function Score()
		{
			this.gameOverSound.load(new URLRequest(Config.ScoreExplPath + "spiel_vorbei.mp3"));
			this.gameEndSound.load(new URLRequest(Config.ScoreExplPath + "auskennen.mp3"));
			this.finishSound.load(new URLRequest(Config.ScoreExplPath + "spiel_neu_starten.mp3"));
		}
		
		public function noMedal(points:int, cart:Cart, gameControl:GameController, buttons:Array):void
		{
			this.gameControl = gameControl;
			this.cart = cart;
			this.cart.startFaceMoving(this.soundChannel);
			this.buttons = buttons;
			this.pointsSound = new Sound;
			this.medalSound = new Sound;

			this.pointsSound.load(new URLRequest(Config.ScorePointsPath + "insgesamt_" + points + ".mp3"));

			this.medalSound.load(new URLRequest(Config.ScoreExplPath + "keine_medaille.mp3"));
			
			this.soundChannel = this.gameOverSound.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playPoints);	
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		public function noMedalNotEnoughPoints(points:int, cart:Cart, gameControl:GameController, buttons:Array):void
		{
			this.gameControl = gameControl;
			this.cart = cart;
			this.buttons = buttons;
			this.pointsSound = new Sound;
			this.medalSound = new Sound;
			
			this.pointsSound.load(new URLRequest(Config.ScorePointsPath + "insgesamt_" + points + ".mp3"));
			this.medalSound.load(new URLRequest(Config.ScoreExplPath + "keine_medaille.mp3"));
			this.pointsSound.addEventListener(Event.COMPLETE, playPoints);
		}
		
		public function medal(points:int, medalColor:String, buttons:Array, cart:Cart, gameControl:GameController):void
		{
			this.gameControl = gameControl;
			this.cart = cart;
			this.buttons = buttons;
			this.pointsSound = new Sound;
			this.medalSound = new Sound;
			this.pictureSound = new Sound;
			this.medalSound.load(new URLRequest(Config.ScoreExplPath + medalColor + ".mp3"));
			this.pointsSound.load(new URLRequest(Config.ScorePointsPath + "insgesamt_" + points + ".mp3"));
			this.pictureSound.load(new URLRequest(Config.ScoreExplPath + "ausmalbild.mp3"));
			picture = true;
			
			this.cart.startFaceMoving(this.soundChannel);
			this.soundChannel = this.gameEndSound.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playPoints);
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function playPoints(e:Event):void
		{
			this.gameControl.showNote(300, 100, false);
			this.pointsSound.removeEventListener(Event.COMPLETE, playPoints);
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, playPoints);
			this.soundChannel = this.pointsSound.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playMedal);
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function playMedal(e:Event):void
		{
			gameControl.showMedal();
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, playMedal);
			this.soundChannel = this.medalSound.play();
			if(picture){
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playPicture);
			}else{
				this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playFinish);
			}
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function playPicture(e:Event):void
		{
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, playPicture);
			this.soundChannel = this.pictureSound.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, playFinish);
			this.cart.startFaceMoving(this.soundChannel);
		}
		
		private function playFinish(e:Event):void
		{
			this.soundChannel.removeEventListener(Event.SOUND_COMPLETE, playFinish);
			this.soundChannel = this.finishSound.play();
			this.soundChannel.addEventListener(Event.SOUND_COMPLETE, showButtons);
			this.cart.startFaceMoving(this.soundChannel);
			
		}
		
		private function showButtons(e:Event):void
		{
			buttons[0].enabled = true;
			buttons[0].alpha = 1;
			buttons[0].filters = [this.gameControl.getPrintGlow()];
			if (picture){
				buttons[1].enabled = true;
				buttons[1].alpha = 1;
				glowPrint(null);
			}
			
		}
		
		private function glowPrint(e:Event):void
		{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, glowPrint);
			this.buttons[1].filters = [this.gameControl.getPrintGlow()];
			timer.start();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
		}
		
		private function timerDone(e:Event):void
		{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
			this.buttons[1].filters = null;
			timer.start();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, glowPrint);
		}
		
	}
}